package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.PersonalCenterBean;
import com.incar.chuxing.bean.ResultBean;
import com.incar.chuxing.utils.AppManager;
import com.incar.chuxing.utils.Constants;
import com.incar.chuxing.utils.ImageLoader;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.SharedPreferencesKey;
import com.incar.chuxing.utils.SharedPreferencesUtils;
import com.incar.chuxing.utils.Toast.ToastCompat;
import com.incar.chuxing.widget.MarqueeView;
import com.incar.chuxing.widget.MyScrollView;
import com.tencent.bugly.beta.Beta;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PersonalCenterActivity extends BaseActivity {

    @BindView(R.id.backdrop)
    ImageView backdrop;
    @BindView(R.id.title_center)
    AutoRelativeLayout title_center;
    @BindView(R.id.scrollView_center)
    MyScrollView scrollview_center;
    @BindView(R.id.scrollView_center_unSign)
    MyScrollView scrollview_centerUnsign;
    @BindView(R.id.myInfo_head)
    CircleImageView myInfoHead;
    @BindView(R.id.center_username)
    TextView centerUsername;
    @BindView(R.id.center_count)
    TextView centerCount;
    @BindView(R.id.center_coupon)
    TextView centerCoupon;
    @BindView(R.id.center_certify_status)
    ImageView center_certify_status;
    @BindView(R.id.myInfo_member)
    ImageView myInfo_member;
    @BindView(R.id.center_back)
    ImageView returnAccount;
    @BindView(R.id.textView7)
    TextView textView7;
    @BindView(R.id.center_unit)
    TextView centerUnit;
    @BindView(R.id.imageView_1)
    ImageView imageView1;
    @BindView(R.id.myInfo_wallet)
    AutoRelativeLayout myInfoWallet;
    @BindView(R.id.center_unit2)
    TextView centerUnit2;
    @BindView(R.id.imageView_2)
    ImageView imageView2;
    @BindView(R.id.myInfo_coupon)
    AutoRelativeLayout myInfoCoupon;
    @BindView(R.id.tv_exchange)
    TextView tvExchange;
    @BindView(R.id.center_unit3)
    TextView centerUnit3;
    @BindView(R.id.imageView_3)
    ImageView imageView3;
    @BindView(R.id.myInfo_exchange)
    AutoRelativeLayout myInfoExchange;
    @BindView(R.id.myInfo_route)
    AutoRelativeLayout myInfoRoute;
    @BindView(R.id.myInfo_invite)
    AutoRelativeLayout myInfoInvite;
    @BindView(R.id.myInfo_guide)
    AutoRelativeLayout myInfoGuide;
    @BindView(R.id.autoLinearLayout)
    AutoLinearLayout autoLinearLayout;
    @BindView(R.id.myInfo_staff)
    AutoRelativeLayout myInfoStaff;
    @BindView(R.id.collapsing_toolbar)
    AutoRelativeLayout collapsingToolbar;
    @BindView(R.id.title_center_title)
    RelativeLayout titleCenterTitle;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.image1)
    ImageView image1;
    @BindView(R.id.image2)
    ImageView image2;
    @BindView(R.id.image3)
    ImageView image3;
    @BindView(R.id.myInfo_message)
    ImageView myInfoMessage;
    @BindView(R.id.main_content)
    AutoRelativeLayout mainContent;
    private Context mContext = this;
    @BindView(R.id.progress_bar_myInfo)
    RelativeLayout progressBarMyInfo;
    @BindView(R.id.center_login)
    TextView center_login;
    @BindView(R.id.mes_red_hot)
    ImageView mesRedHot;
    private int memberType;


    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_center;
    }

    @Override
    public void initData() {

    }

    private void getData() {
        progressBarMyInfo.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getMyInfoData(((MyApplication) getApplication()).getRequestFieldMap()).enqueue(new Callback<PersonalCenterBean>() {
            @Override
            public void onResponse(Call<PersonalCenterBean> call, final Response<PersonalCenterBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    progressBarMyInfo.setVisibility(View.GONE);
                    if (response.body().getStatus() == 200) {
                        SharedPreferencesUtils.getInstance(mContext).putStringValue(SharedPreferencesKey.REAL_NAME_STATUS, response.body().getAttachment().getIdentityStatus() + "");
                        SharedPreferencesUtils.getInstance(mContext).putStringValue(SharedPreferencesKey.LICENSE_CAR_STATUS, response.body().getAttachment().getLicenseStatus() + "");
                        SharedPreferencesUtils.getInstance(mContext).putStringValue(SharedPreferencesKey.DEPOSIT_CAR_STATUS, response.body().getAttachment().getDepositStatus() + "");

                        DecimalFormat df = new DecimalFormat("0.00");
                        centerCount.setText(df.format(response.body().getAttachment().getBalance() / 100));
                        Log.i("centerCount",df.format(response.body().getAttachment().getBalance() / 100));
                        centerCoupon.setText(response.body().getAttachment().getCouponCount() + "");
                        // centerCard.setText(response.body().getAttachment().getCardCouponCount() + "");
                        centerUsername.setText(response.body().getAttachment().getUserName());
                        // tv_exchange.setText(response.body().getAttachment().getUserName());

                        //实名认证状态
                        if (response.body().getAttachment().getIdentityStatus() == 13) {
                            center_certify_status.setImageDrawable(getResources().getDrawable(R.drawable.certify_ok));
                        } else {
                            center_certify_status.setImageDrawable(getResources().getDrawable(R.drawable.certify_no));
                        }
                        //小红点
                        if ("1".equals(response.body().getAttachment().getIsRead())) {
                            mesRedHot.setVisibility(View.VISIBLE);
                        } else {
                            mesRedHot.setVisibility(View.GONE);
                        }


                        //季卡月卡活动图片
                        if (response.body().getAttachment().getIsShowConcessionCard() == 1) {
                            myInfo_member.setVisibility(View.VISIBLE);
                            ImageLoader.getInstance().displayImage(getApplicationContext(),
                                    ((MyApplication) getApplication()).getImageUrl() + response.body().getAttachment().getConcessionCardPicture(), myInfo_member);
                        } else {
                            myInfo_member.setVisibility(View.GONE);
                        }

                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    progressBarMyInfo.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<PersonalCenterBean> call, Throwable t) {
                progressBarMyInfo.setVisibility(View.GONE);
                Log.i("netError", t.toString());
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void initView() {
        scrollview_center.setScrollViewListener(new MyScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (y != 50) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                }
                if (y == 0) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.transparent));
                }
            }
        });
        scrollview_centerUnsign.setScrollViewListener(new MyScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (y != 50) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                }
                if (y == 0) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.transparent));
                }
            }
        });
        myInfoHead.setImageDrawable(getResources().getDrawable(R.drawable.icon_my_head));

    }

    //根据登录的状显示相应的界面
    private void switchLoginStatus() {
        if (((MyApplication) getApplication()).getUid() == null) {
            scrollview_center.setVisibility(View.GONE);
            scrollview_centerUnsign.setVisibility(View.VISIBLE);
        } else {
            scrollview_center.setVisibility(View.VISIBLE);
            scrollview_centerUnsign.setVisibility(View.GONE);
            if (SharedPreferencesUtils.getInstance(mContext).getStringValue("avatarPath") != null) {
                String picPath = SharedPreferencesUtils.getInstance(mContext).getStringValue("avatarPath");
                File picFile = new File(picPath);
                ImageLoader.getInstance().displayCricleImage(getApplicationContext(), picFile, myInfoHead);
            } else if (SharedPreferencesUtils.getInstance(mContext).getStringValue("picture") != null
                    && !SharedPreferencesUtils.getInstance(mContext).getStringValue("picture").equals("0")) {
                ImageLoader.getInstance().displayCricleImage(getApplicationContext(), ((MyApplication) getApplication()).getImageUrl() + SharedPreferencesUtils.getInstance(mContext).getStringValue("picture"), myInfoHead);
            } else {
                myInfoHead.setImageDrawable(getResources().getDrawable(R.drawable.icon_my_head));
            }
            getData();
        }
    }

    @OnClick({R.id.myInfo_head, R.id.center_back, R.id.center_login, R.id.myInfo_wallet, R.id.myInfo_coupon,
            R.id.myInfo_route, R.id.myInfo_message, R.id.myInfo_staff, R.id.myInfo_invite, R.id.myInfo_member, R.id.myInfo_guide, R.id.myInfo_exchange})
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.myInfo_head:
                intent.setClass(mContext, PersonalDataActivity.class);
                startActivity(intent);
                break;
            case R.id.center_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.center_login:
                intent.setClass(mContext, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_wallet:
               /* MobclickAgent.onEvent(this, Constants.PURSE);
                intent.setClass(mContext, WalletActivity.class);
                startActivity(intent);*/

                intent.setClass(mContext, RechargeActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_coupon:
                intent.setClass(mContext, CouponsActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_exchange:
                intent.setClass(mContext, ExchangeCouponsActivity.class);
                startActivity(intent);
                break;

            case R.id.myInfo_route:
                MobclickAgent.onEvent(this, Constants.TRAVEL);
                intent.setClass(mContext, TravelRecordActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_message:
                readAllMsg();
                mesRedHot.setVisibility(View.GONE);
                intent.setClass(mContext, MsgBoxActivity.class);
                startActivity(intent);
                break;

            case R.id.myInfo_staff:
         /*       intent.setClass(mContext, CustomerServiceActivity.class);
                startActivity(intent);
                MobclickAgent.onEvent(this, Constants.CUSTOMER_SERVICE);*/
                break;
            case R.id.myInfo_invite:
                intent.setClass(mContext, InviteFriendActivity.class);
                intent.putExtra(IntentKey.SHARE_TYPE, 2);
                startActivity(intent);
                break;
            case R.id.myInfo_guide:
           /*     intent.setClass(mContext, UserGuideActivity.class);
                startActivity(intent);*/
                String title = "用户指南";
                Intent intent_register = new Intent(getApplicationContext(), WebViewActivity.class);
                intent_register.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, title);
                intent_register.putExtra(IntentKey.INTENT_WEBVIEW_H5TITLE, false);
                intent_register.putExtra(IntentKey.INTENT_WEBVIEW_URL, UrlConstant.Request.USER_GUIDE_URL + "?appVersion=" + ((MyApplication) getApplication()).getAppVersion());
                startActivity(intent_register);
                break;
        }
    }

    private void readAllMsg() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).readAllMsg(((MyApplication) getApplication()).getRequestFieldMap()).enqueue(new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        switchLoginStatus();
    }


}
